package com.chongbangbang.controller.record;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chongbangbang.pojo.DailyInfo;
import com.chongbangbang.pojo.dto.DailyInfoDTO;
import com.chongbangbang.service.DailyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 每日记录之宠物的日常清洁 前端控制器
 * </p>
 *
 * @author TP
 * @since 2022-03-19 15:23:55
 */
@RestController
@RequestMapping("/chong/record/daily")
@Api(tags = "daily info", value = "每日记录")
public class DailyInfoController {

    @Autowired
    private DailyInfoService dailyInfoService;

    @ApiOperation("查询所有")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<DailyInfo> get() {
        return dailyInfoService.list();
    }


    @ApiOperation("根据ID查询")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public DailyInfo getById(@PathVariable Integer id) {
        return dailyInfoService.getById(id);
    }

    @ApiOperation("条件查询")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public List<DailyInfo> getByQueryCondition(DailyInfoDTO dailyInfoDTO) {
        QueryWrapper<DailyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", dailyInfoDTO.getUserId());
        if (!StringUtils.isEmpty(dailyInfoDTO.getType())){
            queryWrapper.eq("type", dailyInfoDTO.getType());
        }
        if (!StringUtils.isEmpty(dailyInfoDTO.getPetNickname())){
            queryWrapper.eq("pet_nickname", dailyInfoDTO.getPetNickname());
        }
        queryWrapper.ge("time", dailyInfoDTO.getBeginTime());
        queryWrapper.le("time", dailyInfoDTO.getEndTime());
        return dailyInfoService.list(queryWrapper);
//        return dailyInfoService.getById(id);
    }


    @ApiOperation("单个插入")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public boolean post(DailyInfo dailyInfo) {
        return dailyInfoService.save(dailyInfo);
    }


    @ApiOperation("批量插入")
    @RequestMapping(value = "/_mcreate", method = RequestMethod.POST)
    public boolean postBatches(@ApiParam(value = "传ArrayList<DailyInfo>") ArrayList<DailyInfo> list) {
        return dailyInfoService.saveBatch(list);
    }


    @ApiOperation("根据ID更新")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public boolean putById(@PathVariable("id") Integer id, DailyInfo dailyInfo) {
        System.out.println("\n\n"+dailyInfo+"\n");
        return dailyInfoService.updateById(dailyInfo);
    }

    @ApiOperation("根据petId更新")
    @RequestMapping(value = "/pet/{petId}", method = RequestMethod.PUT)
    public boolean putByPetId(@PathVariable("petId") Integer petId, DailyInfo dailyInfo) {
        System.out.println("\n\n"+dailyInfo+"\n");
        UpdateWrapper<DailyInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pet_id", petId);
        return dailyInfoService.update(dailyInfo, updateWrapper);
    }

    @ApiOperation("批量更新")
    @RequestMapping(value = "/_mupdate", method = RequestMethod.PUT)
    public boolean putBatches(@ApiParam(value = "传ArrayList<DailyInfo>") ArrayList<DailyInfo> list) {
        return dailyInfoService.updateBatchById(list);
    }


    @ApiOperation("根据主键ID删除")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public boolean deleteById(@PathVariable Integer id) {
        return dailyInfoService.removeById(id);
    }

    @ApiOperation("根据pet_id删除")
    @RequestMapping(value = "/pet/{petId}", method = RequestMethod.DELETE)
    public boolean deleteByPetId(@PathVariable Integer petId) {
        QueryWrapper<DailyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id", petId);
        return dailyInfoService.remove(queryWrapper);
    }

    @ApiOperation("根据ID批量删除")
    @RequestMapping(value = "/_mdelete", method = RequestMethod.DELETE)
    public boolean deleteById(@ApiParam(value = "传ArrayList<Integer>") ArrayList<Integer> list) {
        return dailyInfoService.removeById(list);
    }

}
